package projectServlet;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.StringTokenizer;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import dao.*;
import dao.mysql.EmployeeDAOFactoryImpl;
import dao.mysql.LeaveDAOFactoryImpl;
import dto.LeaveDTO;
import dto.UserDTO;

/**
 * Servlet implementation class StaffDetail
 *
 * @web.servlet
 *   name="StaffDetail"
 *   display-name="StaffDetail"
 *
 * @web.servlet-mapping
 *   url-pattern="/StaffDetailPage"
 */
@WebServlet("/StaffDetailPage")
public class StaffDetailPage extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

			doPreProcess(request,response);

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPreProcess(request,response);	
	}
	
	protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		
	}
	protected void doPreProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		EmployeeDAOFactory eFactory = new EmployeeDAOFactoryImpl();
		LeaveDAOFactory leaveDAO = new LeaveDAOFactoryImpl();
		LeaveDTO Leave = new LeaveDTO();		
		
		String SuserID = (String)request.getSession().getAttribute("id");
		int userID     = Integer.parseInt(SuserID);
		int leaveID = (int) request.getSession().getAttribute("TransferLeaveID");	

		
		Leave.set_leaveID(leaveID);
		String str1 = request.getParameter("Update");
		String str2 = request.getParameter("Delete");
		String str3 = request.getParameter("Cancel");
		
		if(str1 != null && str1.equals("Update"))
		{
			//need validation
			
			String sd = (String) request.getAttribute("startdate");
			String se = (String) request.getAttribute("enddate");
			
			Date sdate = ConvertStringToDate(sd);
			Date edate = ConvertStringToDate(se);
			
			Leave.set_startDate(sdate);
			Leave.set_endDate(edate);
			
			Leave.set_startDateHalfDay(request.getParameter("startdateHalf"));
			Leave.set_startDateHalfDay(request.getParameter("enddateHalf"));
			
			Leave.set_reason(request.getParameter("reason"));
			Leave.set_workDissemination(request.getParameter("workdiss"));	
			try {
				Leave.set_ApprovingManID(eFactory.getEmployeeDAO().findUser(userID).get_supID());
			} catch (DAOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			Leave.set_status("Pending");
			Leave.set_applicationDate(Calendar.getInstance().getTime());
			
			try {
				leaveDAO.getLeaveDAO().updateLeave(Leave);
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		
		if(str2 != null && str2.equals("Delete"))
		{	
			try {
				leaveDAO.getLeaveDAO().deleteLeave(Leave);
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
			
		if(str3 != null && str3.equals("Cancel"))
		{
			try {
				leaveDAO.getLeaveDAO().removeFromDatabase(Leave);
			} catch (DAOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		RequestDispatcher rd = request.getRequestDispatcher("/viewdetail");
		rd.forward(request, response);
	}
	private Date ConvertStringToDate(String OringalString)
	{
		StringTokenizer st = new StringTokenizer(OringalString,"-");
		int Year = 0,Month = 0,Day = 0;
		int i = 0;
		while(st.hasMoreTokens())
		{
			switch(i)
			{
				case 0:
					Year = Integer.parseInt(st.nextToken());					
					break;
				case 1:
					Month = Integer.parseInt(st.nextToken());		
					break;
				case 2:
					Day = Integer.parseInt(st.nextToken());		
					break;
			}
			i++;

		}
		Calendar cl = Calendar.getInstance();
		cl.set(Year, Month, Day);
		return cl.getTime();
	}
}